/**
 * 
 */
package com.android.fxcontacts;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.provider.ContactsContract;

/**
 * /**
 * A convenience class that helps launch contact search from within the app.
 */
public class ContactsSearchManager
{
	/**
	 * An extra that provides context for search UI and defines the scope for
	 * the search queries.
	 */
	public static final String ORIGINAL_ACTION_EXTRA_KEY = "originalAction";

	/**
	 * An extra that provides context for search UI and defines the scope for
	 * the search queries.
	 */
	public static final String ORIGINAL_COMPONENT_EXTRA_KEY = "originalComponent";

	// ContactsConstract.Intents.UI constant string
	private static final String FILTER_CONTACTS_ACTION = "com.android.contacts.action.FILTER_CONTACTS";
	private static final String FILTER_TEXT_EXTRA_KEY = "com.android.contacts.extra.FILTER_TEXT";

	/**
	 * Starts the contact list activity in the search mode.
	 */
	public static void startSearch(Activity context, String initialQuery)
	{
		context.startActivity(buildIntent(context, initialQuery));
	}

	public static void startSearchForResult(Activity context,
			String initialQuery, int requestCode)
	{
		context.startActivityForResult(buildIntent(context, initialQuery),
				requestCode);
	}

	private static Intent buildIntent(Activity context, String initialQuery)
	{
		Intent intent = new Intent();
		intent.setData(ContactsContract.Contacts.CONTENT_URI);
		intent.setAction(FILTER_CONTACTS_ACTION);

		Intent originalIntent = context.getIntent();
		Bundle originalExtras = originalIntent.getExtras();
		if (originalExtras != null)
		{
			intent.putExtras(originalExtras);
		}
		intent.putExtra(FILTER_TEXT_EXTRA_KEY, initialQuery);
		intent.putExtra(ORIGINAL_ACTION_EXTRA_KEY, originalIntent.getAction());
		intent.putExtra(ORIGINAL_COMPONENT_EXTRA_KEY, originalIntent
				.getComponent().getClassName());
		return intent;
	}

}
